/**
 * JAVACC DEMO 1.0
 */
package com.apache.info.util;

import java.util.Locale;

import com.apache.rpc.entity.RpcDatasource;
import org.springframework.context.NoSuchMessageException;

import com.apache.cache.service.impl.LoadCacheFactory;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SpringContextLoader;
import com.apache.tools.StrUtil;

/**
 * description:
 *
 */
public class MessageUtil {

	public static String getMessage(String key, String[] params, String defaultMessage) {
		String msg = "";
		try {
			msg = SpringContextLoader.getContext().getMessage(key, params, Locale.CHINA);
			msg = Validator.getDefaultStr(msg, defaultMessage);
		} catch (NoSuchMessageException e) {
			msg = defaultMessage;
		}
		return Validator.getDefaultStr(msg, defaultMessage);
	}

	public static String getDatabastType(String sysName, String driverClassName) {
		if (StrUtil.isNull(sysName)) {
			if ("oracle.jdbc.OracleDriver".equals(driverClassName)) {
				return "oracle";
			} else if ("com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driverClassName)) {
				return "sqlserver";
			} else {
				return "mysql";
			}
		} else {
			String key = "rpc_datasource_" + sysName;
			Object obj = LoadCacheFactory.getInstance().getCacheManager("").getCacheObjectByKey(key);
			if (null == obj) {
				obj = LoadCacheFactory.getInstance().getCacheManager("")
						.getCacheObjectByKey("rpc_datasource_plateform");
			}
			if (null == obj) {
				return "mysql";
			}
			RpcDatasource rds = (RpcDatasource) obj;
			return StrUtil.doNull(rds.getDatabaseType(), "mysql");
		}
	}

	/**
	 * 获取动态表单缓存KEY
	 */
	public static String getTableCacheKey(String sortCode, String sysName, String suffix) {
		return sortCode + "_" + StrUtil.doNull(sysName, "plateform") + suffix;
	}
}
